package com.wac.common.config;

import feign.Response;
import feign.codec.ErrorDecoder;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

/**
 * @author wuancheng
 * @date 2024/8/26 15:32
 * @description 除此定义捕获异常外，还可使用全局异常@ExceptionHandler捕获返回响应，并注意捕获的exception类型
 */
@Component
public class CustomErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String methodKey, Response response) {
        HttpStatus status = HttpStatus.valueOf(response.status());
        switch (status) {
            case NOT_FOUND:
                return new RuntimeException("服务未找到");
            case INTERNAL_SERVER_ERROR:
                return new RuntimeException("服务器内部异常");
            default:
                return new RuntimeException("系统异常");
        }
    }
}
